package com.xywy.fwc.adapter;

import java.util.Calendar;
import java.util.List;

import com.xywy.R;
import com.xywy.fwc.bean.FwcMessageBean;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

public class FwcMessageAdapter extends BaseAdapter {
	List<FwcMessageBean> messages;
	Context context;
	Calendar calendar;
	public FwcMessageAdapter(List<FwcMessageBean> messages, Context context) {
		super();
		this.messages = messages;
		this.context = context;
		calendar = Calendar.getInstance();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return messages.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		// TODO Auto-generated method stub
		FwcMessageBean fwb = messages.get(position);
		ViewHolder holder = null;
		if(convertView == null){
			convertView = View.inflate(context, R.layout.fwc_message_item, null);
			holder = new ViewHolder();
			holder.title = (TextView) convertView.findViewById(R.id.title);
			holder.name = (TextView) convertView.findViewById(R.id.name);
			holder.ivTime = (ImageView) convertView.findViewById(R.id.ivTime);
			holder.time = (TextView) convertView.findViewById(R.id.time);
			convertView.setTag(holder);
		}else {
			holder = (ViewHolder) convertView.getTag();
		}
		if(fwb.isFlag()){
			//已读
			convertView.setBackgroundColor(0xffF0EFED);
			holder.ivTime.setBackgroundResource(R.drawable.fwc_time_normal);
		}else {
			//未读
			convertView.setBackgroundColor(0xffffffff);
			holder.ivTime.setBackgroundResource(R.drawable.fwc_time_press);
		}
		holder.name.setText(fwb.getService_provider_name());
		holder.title.setText(fwb.getTitle());
		if(fwb.getDatetime() != null){
			long time = Long.parseLong(fwb.getDatetime())*1000;
			calendar.setTimeInMillis(time);
			String date = calendar.get(Calendar.MONTH)+"月"+calendar.get(Calendar.DAY_OF_MONTH)+
					"日"+calendar.get(Calendar.HOUR_OF_DAY)+"时"+calendar.get(Calendar.MINUTE)+"分";
			holder.time.setText(date);
		}
		convertView.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT,dip2px(context, 80)));
		return convertView;
	}
	
	class ViewHolder{
	   public TextView title;
	   public TextView name;
	   public TextView time;
	   public ImageView ivTime;
	}

	/**
	 * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
	 */
	public static int dip2px(Context context, float dpValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}
}
